package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelsService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("travel")
public class TravelController {
    @Autowired
    private ITravelsService travelsService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelQuery qo, HttpServletRequest request){
        String token =  request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        Page<Travel> page = travelsService.query(qo, user);

        model.addAttribute("page",page);
        return "/travel/list";
    }
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id,int state){
        travelsService.changeState(id,state);
        return JsonResult.success();
    }
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){
        Travel travel = travelsService.get(id);
        return JsonResult.success(travel.getContent());
    }

}
